package com.songlanyun.modules.officialDonation.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.enums.DonationConstant;
import com.songlanyun.common.enums.IntegralConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.Constant;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;
import com.songlanyun.common.utils.custom.MathUtils;
import com.songlanyun.modules.exception.DonationExceptionEnum;
import com.songlanyun.modules.integralrecord.entity.IntegralRecord;
import com.songlanyun.modules.integralrecord.service.IntegralRecordService;
import com.songlanyun.modules.officialDonation.dao.OfficialDonationDao;
import com.songlanyun.modules.officialDonation.entity.OfficialDonationEntity;
import com.songlanyun.modules.officialDonation.service.OfficialDonationService;
import com.songlanyun.modules.pointsexchangeconfig.entity.PointsExchangeConfigEntity;
import com.songlanyun.modules.pointsexchangeconfig.service.PointsExchangeConfigService;
import com.songlanyun.modules.publicbenefitmsg.model.to.PublicBenefitMsgTO;
import com.songlanyun.modules.publicbenefitmsg.service.PublicBenefitMsgService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;


@Service("officialDonationService")
public class OfficialDonationServiceImpl extends ServiceImpl<OfficialDonationDao, OfficialDonationEntity> implements OfficialDonationService {

    @Resource
    private PointsExchangeConfigService pointsExchangeConfigService;

    @Resource
    private IntegralRecordService integralRecordService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OfficialDonationEntity> page = this.baseMapper.queryPage(new Query<OfficialDonationEntity>().getPage(params), params);

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OfficialDonationEntity audit(OfficialDonationEntity officialDonation) {
        OfficialDonationEntity entity = this.loadById(officialDonation.getId(), true);
        if (Constant.AUDITWAIT != entity.getAuditStatus()) {
            throw new RRException(DonationExceptionEnum.CURRENT_OFFICIAL_DONATION_RECORDS_HAVE_BEEN_REVIEWED);
        }

        //修改审核状态
        entity.setAuditStatus(officialDonation.getAuditStatus());
        entity.setAuditor(officialDonation.getAuditor());
        entity.setAuditTime(new Date());
        entity.setSerialNumber(officialDonation.getSerialNumber());

        //审核通过
        if (Constant.AUDITPASS == entity.getAuditStatus()) {
            //获得公益积分
            PointsExchangeConfigEntity config = pointsExchangeConfigService.loadConfig(false);
            BigDecimal issuedQuantity = BigDecimal.ZERO;
            if (ObjectUtil.isNotNull(config)) {
                issuedQuantity = MathUtils.multiply(entity.getDonationAmount(), config.getDonationExchangeRatio());
            }
            entity.setIssuedQuantity(issuedQuantity);

            //保存积分明细
//            IntegralRecord record = new IntegralRecord(entity.getUserId(), entity.getIssuedQuantity(), IntegralConstant.InnerMode.OFFICIAL_DONATION,
//                    IntegralConstant.IntegralModelType.OFFICIAL_DONATION, entity.getId(), "官方捐赠获得积分");
//            integralRecordService.inner(record);
        }

        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public OfficialDonationEntity loadById(Long id, boolean tw) {
        OfficialDonationEntity entity = this.baseMapper.selectById(id);
        if (ObjectUtil.isNull(entity) && tw) {
            throw new RRException(DonationExceptionEnum.OFFICIAL_DONATION_RECORDS_DO_NOT_EXIST);
        }
        return entity;
    }
}
